package com.xls.pet.controller;

import com.mybatisflex.core.paginate.Page;
import com.xls.pet.common.Result;
import com.xls.pet.entity.Item;
import com.xls.pet.query.ItemQuery;
import com.xls.pet.service.ItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 控制层。
 *
 * @author xls
 * @since 2024-06-17
 */
@RestController
@RequestMapping("/item")
public class ItemController {

    @Autowired
    private ItemService itemService;

    @GetMapping("/{id}")
    public Result<?> getItem(@PathVariable Integer id) {
        return Result.success(itemService.getById(id));
    }

    /**
     * 添加。
     */
    @PostMapping("save")
    public Result<?> save(@RequestBody Item item) {
        itemService.saveItem(item);
        return Result.success();
    }

    /**
     * 根据主键删除。
     *
     * @param ids 主键
     */
    @DeleteMapping("remove/{ids}")
    public Result<?> remove(@PathVariable Integer[] ids) {
        itemService.removeItem(ids);
        return Result.success();
    }

    /**
     * 根据主键更新。
     */
    @PutMapping("update")
    public Result<?> update(@RequestBody Item item) {
        itemService.updateItem(item);
        return Result.success();
    }

    @PostMapping("/list/{pageNumber}/{pageSize}/{totalRow}")
    public Result<Page<Item>> list(@PathVariable Integer pageNumber, @PathVariable Integer pageSize,
                                   @PathVariable Integer totalRow, @RequestBody ItemQuery itemQuery) {
        return Result.success(itemService.list(pageNumber, pageSize, totalRow, itemQuery));
    }

}
